"""
物联网卡管理相关接口
包含单卡详情、在线信息、流量查询、激活、批量查询、网络状态、历史用量、销户、停机原因、IMEI、日流量等接口方法。
"""

from .api_utils import md5, get_tkey, post_api

def single_card_detail_query(userName, passWord, iccid=None, msisdn=None):
    """单张卡详情查询接口"""
    url = "https://api.tibiot.com/api/v1/card/queryCardInfo"
    tKey = get_tkey()
    pw = md5(md5(passWord) + tKey)
    data = {"userName": userName, "passWord": pw, "tKey": tKey}
    if iccid:
        data["iccid"] = iccid
    elif msisdn:
        data["msisdn"] = msisdn
    else:
        return {"error": "iccid或msisdn必填其一"}
    return post_api(url, data)

def card_online_info_query(userName, passWord, iccid=None, msisdn=None):
    """单张卡在线信息实时查询接口"""
    url = "https://api.tibiot.com/api/v1/card/cardOnlineInformation"
    tKey = get_tkey()
    pw = md5(md5(passWord) + tKey)
    data = {"userName": userName, "passWord": pw, "tKey": tKey}
    if iccid:
        data["iccid"] = iccid
    elif msisdn:
        data["msisdn"] = msisdn
    else:
        return {"error": "iccid或msisdn必填其一"}
    return post_api(url, data)

def card_monthly_flow_query(userName, passWord, iccid=None, msisdn=None):
    """单张卡本月流量使用量查询接口"""
    url = "https://api.tibiot.com/api/v1/card/simDataMargin"
    tKey = get_tkey()
    pw = md5(md5(passWord) + tKey)
    data = {"userName": userName, "passWord": pw, "tKey": tKey}
    if iccid:
        data["iccid"] = iccid
    elif msisdn:
        data["msisdn"] = msisdn
    else:
        return {"error": "iccid或msisdn必填其一"}
    return post_api(url, data)

def card_force_activate(userName, passWord, iccid):
    """卡强制激活接口"""
    url = "https://api.tibiot.com/api/v1/card/cardActivation"
    tKey = get_tkey()
    pw = md5(md5(passWord) + tKey)
    data = {"userName": userName, "passWord": pw, "tKey": tKey, "iccid": iccid}
    return post_api(url, data)

def card_batch_info_query(userName, passWord, iccid_list = None,pageSize=100,pageNum=1):
    """卡信息批量查询接口"""
    url = "https://api.tibiot.com/api/v1/card/batchQueryCardInfo"
    tKey = get_tkey()
    pw = md5(md5(passWord) + tKey)
    data = {"userName": userName, "passWord": pw, "tKey": tKey,"pageSize":pageSize,"pageNum":pageNum}
    if iccid_list is None:
        data["cardType"] = 2
    else:
        data["iccids"] = iccid_list
    return post_api(url, data)

def multiple_cards_query(userName, passWord, iccid_list):
    """多张卡信息查询接口"""
    url = "https://api.tibiot.com/api/v1/card/moreQueryCardInfo"
    tKey = get_tkey()
    pw = md5(md5(passWord) + tKey)
    data = {"userName": userName, "passWord": pw, "tKey": tKey, 
            "iccids": iccid_list
            }
    print(f"批量接口请求数据: {data}")
    return post_api(url, data)

def network_status_query(userName, passWord, iccid):
    """网络状态查询接口"""
    url = "https://api.tibiot.com/api/v1/card/getCardNetWork"
    tKey = get_tkey()
    pw = md5(md5(passWord) + tKey)
    data = {"userName": userName, "passWord": pw, "tKey": tKey, "iccid": iccid}
    return post_api(url, data)

def network_handle(userName, passWord, iccid, operateType):
    """操作网络状态接口"""
    url = "https://api.tibiot.com/api/v1/card/cardNetWork"
    tKey = get_tkey()
    pw = md5(md5(passWord) + tKey)
    data = {"userName": userName, "passWord": pw, "tKey": tKey, "iccid": iccid, "operateType": operateType}
    return post_api(url, data)

def card_history_monthly_usage(userName, passWord, iccid):
    """卡历史月用量查询接口"""
    url = "https://api.tibiot.com/api/v1/card/queryCardMonthData"
    tKey = get_tkey()
    pw = md5(md5(passWord) + tKey)
    data = {"userName": userName, "passWord": pw, "tKey": tKey, "iccid": iccid}
    return post_api(url, data)

def card_account_cancel_submit(userName, passWord, iccid):
    """卡销户申请提交接口"""
    url = "https://api.tibiot.com/api/v1/card/applyDisassemble"
    tKey = get_tkey()
    pw = md5(md5(passWord) + tKey)
    data = {"userName": userName, "passWord": pw, "tKey": tKey, "iccid": iccid}
    return post_api(url, data)

def card_account_cancel_query(userName, passWord, iccid):
    """卡销户申请查询接口"""
    url = "https://api.tibiot.com/api/v1/card/disassembleInfo"
    tKey = get_tkey()
    pw = md5(md5(passWord) + tKey)
    data = {"userName": userName, "passWord": pw, "tKey": tKey, "iccid": iccid}
    return post_api(url, data)

def card_shutdown_reason_query(userName, passWord, iccid):
    """卡停机原因查询接口"""
    url = "https://api.tibiot.com/api/v1/card/getCardStopReason"
    tKey = get_tkey()
    pw = md5(md5(passWord) + tKey)
    data = {"userName": userName, "passWord": pw, "tKey": tKey, "iccid": iccid}
    return post_api(url, data)

def card_imei_query(userName, passWord, iccid):
    """卡使用终端IMEI查询接口"""
    url = "https://api.tibiot.com/api/v1/card/querySimIMEI"
    tKey = get_tkey()
    pw = md5(md5(passWord) + tKey)
    data = {"userName": userName, "passWord": pw, "tKey": tKey, "iccid": iccid}
    return post_api(url, data)

def card_day_flow_query(userName, passWord, iccid, date):
    """单日数据流量查询接口"""
    url = "https://api.tibiot.com/api/v1/card/newIotData"
    tKey = get_tkey()
    pw = md5(md5(passWord) + tKey)
    data = {"userName": userName, "passWord": pw, "tKey": tKey, "iccid": iccid, "date": date}
    return post_api(url, data)

def card_day_flow_batch_query(userName, passWord, iccid_list, date):
    """单日数据流量查询(批量)接口"""
    url = "https://api.tibiot.com/api/v1/card/batchNewIotData"
    tKey = get_tkey()
    pw = md5(md5(passWord) + tKey)
    data = {"userName": userName, "passWord": pw, "tKey": tKey, "iccid": iccid_list, "date": date}
    return post_api(url, data)
